package com.toocms.jinjuhang.ui.mine.payment.bank.add;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.toocms.jinjuhang.config.Constants;
import com.toocms.jinjuhang.config.UserRepository;
import com.toocms.tab.base.BaseModel;
import com.toocms.tab.base.BaseViewModel;
import com.toocms.tab.binding.command.BindingCommand;
import com.toocms.tab.bus.Messenger;
import com.toocms.tab.bus.event.SingleLiveEvent;
import com.toocms.tab.network.ApiTool;

/**
 * @Description:
 * @Author: Zero
 * @CreateDate: 2023/5/19 9:59
 */
public class AddBankViewModel extends BaseViewModel<BaseModel> {

    public ObservableField<String> card = new ObservableField<>();
    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> no = new ObservableField<>();

    public SingleLiveEvent<String> showCodeEvent = new SingleLiveEvent<>();

    public AddBankViewModel(@NonNull Application application) {
        super(application);
        Messenger.getDefault().register(this, Constants.MessengerToken.TOKEN_CODE, () -> {
            finishFragment();
        });
    }

    public BindingCommand dismiss = new BindingCommand(this::finishFragment);

    public BindingCommand save = new BindingCommand(() -> {
        if (StringUtils.isEmpty(card.get())) {
            showToast("请输入银行卡号");
            return;
        } else if (StringUtils.isEmpty(name.get())) {
            showToast("请输入姓名");
            return;
        } else if (StringUtils.isEmpty(phone.get())) {
            showToast("请输入预留手机号");
            return;
        } else if (StringUtils.isEmpty(no.get())) {
            showToast("请输入证件号码");
            return;
        }
        signProtocol();
    });

    private void signProtocol() {
        ApiTool.post("/Center/signProtocol")
                .add("member_id", UserRepository.INSTANCE.getUser().getMember_id())
                .add("payerBankAccountNo", card.get())
                .add("payerBankAccountName", name.get())
                .add("payerPhone", phone.get())
                .add("payerCredentialNo", no.get())
                .asTooCMSResponse(String.class)
                .withViewModel(this)
                .request(s -> {
                    showCodeEvent.postValue(JsonUtils.getString(s, "signid"));
                });
    }
}
